package de.bk.timecalc.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuBar
{
  private GameWindowBehaviour behaviour;

  public MenuBar(GameWindowBehaviour behaviour)
  {
    this.behaviour = behaviour;
  }

  void initMenu(Shell shell)
  {
    Menu menuBar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menuBar);

    Menu menuDropDownGame = new Menu(shell, SWT.DROP_DOWN);
    MenuItem menuDropDownItemGame = new MenuItem(menuBar, SWT.CASCADE);
    menuDropDownItemGame.setMenu(menuDropDownGame);
    menuDropDownItemGame.setText("&Game");

    this.createMenuItem(menuDropDownGame, "&Reset\tCtrl+R", SWT.CONTROL | 'R')
        .addListener(SWT.Selection, new Listener()
        {
          public void handleEvent(Event e)
          {
            MenuBar.this.behaviour.reset();
          }
        });

    this.createMenuItem(menuDropDownGame, "&Pause/Continue\tCtrl+P",
        SWT.CONTROL | 'P').addListener(SWT.Selection, new Listener()
    {
      public void handleEvent(Event e)
      {
        MenuBar.this.behaviour.togglePause();
      }
    });

    Menu menuDropDownOptions = new Menu(shell, SWT.DROP_DOWN);
    MenuItem menuDropDownItemOptions = new MenuItem(menuBar, SWT.CASCADE);
    menuDropDownItemOptions.setMenu(menuDropDownOptions);
    menuDropDownItemOptions.setText("&Options");

    this.createMenuItem(menuDropDownOptions, "&Settings\tCtrl+X",
        SWT.CONTROL | 'X').addListener(SWT.Selection, new Listener()
    {
      public void handleEvent(Event e)
      {
        MenuBar.this.behaviour.openSettingsWindow();
      }
    });

    Menu menuDropDownHelp = new Menu(shell, SWT.DROP_DOWN);
    MenuItem menuDropDownItemHelp = new MenuItem(menuBar, SWT.CASCADE);
    menuDropDownItemHelp.setMenu(menuDropDownHelp);
    menuDropDownItemHelp.setText("&Help");
    this.createMenuItem(menuDropDownHelp, "&Help\tCtrl+H", SWT.CONTROL | 'H')
        .addListener(SWT.Selection, new Listener()
        {
          public void handleEvent(Event e)
          {
            MenuBar.this.behaviour.openHelpWindow();
          }
        });
    this.createMenuItem(menuDropDownHelp, "&Project Website").addListener(
        SWT.Selection, new Listener()
        {
          public void handleEvent(Event e)
          {
            Program.launch("http://code.google.com/p/qltimecalc/");
          }
        });

    this.createMenuItem(menuDropDownHelp, "&About\tCtrl+A", SWT.CONTROL | 'A')
        .addListener(SWT.Selection, new Listener()
        {
          public void handleEvent(Event e)
          {
            MenuBar.this.behaviour.openAboutWindow();
          }
        });
  }

  private MenuItem createMenuItem(Menu menu, String text)
  {
    MenuItem menuItemFileNew = new MenuItem(menu, SWT.PUSH);
    menuItemFileNew.setText(text);
    return menuItemFileNew;
  }

  private MenuItem createMenuItem(Menu menu, String text, int accelerator)
  {
    MenuItem menuItemFileNew = createMenuItem(menu, text);
    menuItemFileNew.setAccelerator(accelerator);
    return menuItemFileNew;
  }
}
